package com.castle.fortress.admin.dify.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.castle.fortress.admin.dify.mapper.ContractReviewInfoMapper;
import com.castle.fortress.admin.dify.model.ContractReviewInfo;
import com.castle.fortress.admin.dify.service.ContractReviewInfoService;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ContractReviewInfoServiceImpl extends ServiceImpl<ContractReviewInfoMapper, ContractReviewInfo> 
    implements ContractReviewInfoService {

    @Override
    public List<ContractReviewInfo> getByContractId(Long contractId) {
        return list(new LambdaQueryWrapper<ContractReviewInfo>()
                .eq(ContractReviewInfo::getContractId, contractId)
                .orderByDesc(ContractReviewInfo::getReviewStartTime));
    }

    @Override
    public boolean startReview(Long contractId, Long ruleId) {
        ContractReviewInfo info = new ContractReviewInfo();
        info.setContractId(contractId);
        info.setRuleId(ruleId);
        info.setReviewStatus("REVIEWING");
        info.setReviewStartTime(DateUtil.now());
        return save(info);
    }

    @Override
    public boolean completeReview(Long id) {
        ContractReviewInfo info = getById(id);
        if (info != null) {
            info.setReviewStatus("COMPLETED");
            info.setReviewEndTime(DateUtil.now());
            return updateById(info);
        }
        return false;
    }
} 